package com.fsnbigdata.service.impl;
import com.fsnbigdata.domain.Park;
import com.fsnbigdata.domain.ZhKopp;
import com.fsnbigdata.domain.dto.NamingPair;
import com.fsnbigdata.repository.VegetationImageRepository;
import com.fsnbigdata.service.api.VegetationService;
import com.fsnbigdata.util.ObjectNull;
import com.fsnbigdata.vo.NewClass;
import com.querydsl.core.types.Predicate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.roo.addon.layers.service.annotations.RooServiceImpl;
import com.fsnbigdata.domain.ListQuadrat;
import com.fsnbigdata.domain.Vegetation;
import com.fsnbigdata.repository.VegetationRepository;
import com.fsnbigdata.service.api.ListQuadratService;
import io.springlets.data.domain.GlobalSearch;

import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;

/**
 * = VegetationServiceImpl
 *
 * TODO Auto-generated class documentation
 *
 */
@RooServiceImpl(service = VegetationService.class)
@Service
@Transactional(readOnly = true)
public class VegetationServiceImpl implements VegetationService {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private ListQuadratService listQuadratService;

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private VegetationRepository vegetationRepository;

    private VegetationImageRepository vegetationImageRepository;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param vegetationRepository
     * @param listQuadratService
     */
    @Autowired
    public VegetationServiceImpl(ListQuadratService listQuadratService, VegetationRepository vegetationRepository, VegetationImageRepository vegetationImageRepository) {
        this.listQuadratService = listQuadratService;
        this.vegetationRepository = vegetationRepository;
        this.vegetationImageRepository = vegetationImageRepository;
    }


//    public VegetationServiceImpl(VegetationRepository vegetationRepository, @Lazy ListQuadratService listQuadratService) {
//        setVegetationRepository(vegetationRepository);
//        setListQuadratService(listQuadratService);
//    }

    public VegetationImageRepository getVegetationImageRepository() {
        return vegetationImageRepository;
    }

    public void setVegetationImageRepository(VegetationImageRepository vegetationImageRepository) {
        this.vegetationImageRepository = vegetationImageRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return VegetationRepository
     */
    public VegetationRepository getVegetationRepository() {
        return vegetationRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param vegetationRepository
     */
    public void setVegetationRepository(VegetationRepository vegetationRepository) {
        this.vegetationRepository = vegetationRepository;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return ListQuadratService
     */
    public ListQuadratService getListQuadratService() {
        return listQuadratService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param listQuadratService
     */
    public void setListQuadratService(ListQuadratService listQuadratService) {
        this.listQuadratService = listQuadratService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param vegetation
     * @param listQuadratToAdd
     * @return Vegetation
     */
    @Transactional
    public Vegetation addToListQuadrat(Vegetation vegetation, Iterable<ListQuadrat> listQuadratToAdd) {
        vegetation.addToListQuadrat(listQuadratToAdd);
        return getVegetationRepository().save(vegetation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param vegetation
     * @param listQuadratToRemove
     * @return Vegetation
     */
    @Transactional
    public Vegetation removeFromListQuadrat(Vegetation vegetation, Iterable<Long> listQuadratToRemove) {
        List<ListQuadrat> listQuadrat = getListQuadratService().findAll(listQuadratToRemove);
        vegetation.removeFromListQuadrat(listQuadrat);
        return getVegetationRepository().save(vegetation);
    }

    @Override
    public List<NamingPair> findAllByInitial(String initial) {
        return getVegetationRepository().findAllByInitial(initial);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param vegetation
     */
    @Transactional
    public void delete(Vegetation vegetation) {
        // Clear bidirectional one-to-many parent relationship with ListQuadrat
        for (ListQuadrat item : vegetation.getListQuadrat()) {
            item.setVegetation(null);
        }
        getVegetationRepository().delete(vegetation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entities
     * @return List
     */
    @Transactional
    public List<Vegetation> save(Iterable<Vegetation> entities) {
        return getVegetationRepository().save(entities);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     */
    @Transactional
    public void delete(Iterable<Long> ids) {
        List<Vegetation> toDelete = getVegetationRepository().findAll(ids);
        getVegetationRepository().deleteInBatch(toDelete);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param entity
     * @return Vegetation
     */
    @Transactional
    public Vegetation save(Vegetation entity) {
        return getVegetationRepository().save(entity);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return Vegetation
     */
    public Vegetation findOne(Long id) {
        return getVegetationRepository().findOne(id);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return Vegetation
     */
    public Vegetation findOneForUpdate(Long id) {
        return getVegetationRepository().findOneDetached(id);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @return List
     */
    public List<Vegetation> findAll(Iterable<Long> ids) {
        return getVegetationRepository().findAll(ids);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return List
     */
    public List<Vegetation> findAll() {
        return getVegetationRepository().findAll();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Long
     */
    public long count() {
        return getVegetationRepository().count();
    }

    @Override
    public Page<Vegetation> search(Predicate predicate, Pageable pageable) {
        return getVegetationRepository().findAll(predicate, pageable);
    }

    @Override
    public Page<Vegetation> findAll(GlobalSearch globalSearch, Pageable pageable, String creator) {
        return getVegetationRepository().findAll(globalSearch, pageable, creator);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Vegetation> findAll(GlobalSearch globalSearch, Pageable pageable) {
        return getVegetationRepository().findAll(globalSearch, pageable, null);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param ids
     * @param globalSearch
     * @param pageable
     * @return Page
     */
    public Page<Vegetation> findAllByIdsIn(List<Long> ids, GlobalSearch globalSearch, Pageable pageable) {
        return getVegetationRepository().findAllByIdsIn(ids, globalSearch, pageable);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    public Class<Vegetation> getEntityType() {
        return Vegetation.class;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return Class
     */
    public Class<Long> getIdType() {
        return Long.class;
    }

    @Override
    public List<List<Vegetation>> findAllByCategory() {
        List<List<Vegetation>> list = new ArrayList<>();
        for (Integer i=0;i<2;i++){
            if (i == 0){
                List<Vegetation> vegetations = getVegetationRepository().findAllByCategory("自然植被");
                if(vegetations!=null&&vegetations.size()>4){
                    list.add(vegetations.subList( 0,4 )) ;
                }else {
                    list.add(vegetations);
                }
            }
            if (i == 1){
                List<Vegetation> vegetations = getVegetationRepository().findAllByCategory("人工植被");
                if(vegetations!=null&&vegetations.size()>4){
                    list.add(vegetations.subList( 0,4 )) ;
                }else {
                    list.add(vegetations);
                }
            }

        }
        return list;
    }

    @Override
    public List findByPageAndSize(String category,String location, String name, Pageable pageable) {
        class Demo extends NewClass{
            String habit;
            String length;
            String temperature;
            String precipitation;

            public String getTemperature() {
                return temperature;
            }

            public void setTemperature(String temperature) {
                this.temperature = temperature;
            }

            public String getPrecipitation() {
                return precipitation;
            }

            public void setPrecipitation(String precipitation) {
                this.precipitation = precipitation;
            }

            public String getHabit() {
                return habit;
            }

            public void setHabit(String habit) {
                this.habit = habit;
            }

            public String getLength() {
                return length;
            }

            public void setLength(String length) {
                this.length = length;
            }
        }
        List list = new ArrayList(  );
        List<Demo> vegetayions = new ArrayList <>(  );
        List<Long> ids = vegetationRepository.queryIds( location );
        String sign = null;
        if(ids.size()>0){
            sign = "ok";
        }
        list.add( vegetationRepository.queryCountByKey( name,category,sign,ids ) );
        for (Object o:vegetationRepository.queryAllByKey( name,category,sign,ids,pageable )){
            Object[] objects = (Object[]) o;
            Demo demo = new Demo();
            demo.setId( ObjectNull.takeOutNull( objects[0] ) );
            demo.setName( ObjectNull.takeOutNull(objects[1]) );
            demo.setTemperature( ObjectNull.takeOutNull(objects[2]) );
            demo.setHabit( ObjectNull.takeOutNull(objects[3]) );
            demo.setLength( ObjectNull.takeOutNull(objects[4]) );
            demo.setPrecipitation( ObjectNull.takeOutNull(objects[5]) );
            List<String> url = vegetationImageRepository.getUrl( ""+objects[0] );
            demo.setUrl( url.size()>0?url.get( 0 ):null );
            vegetayions.add( demo );
        }
        list.add( vegetayions );
        return list;
    }
}
